package com.nyt.org.modules.OrderCancel.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.nyt.org.common.utils.JsonConvert;
import com.nyt.org.common.utils.SendPostRequest;
import com.nyt.org.common.utils.xml.XmlUtils;
import com.nyt.org.modules.OrderCancel.model.REQ2011;
import com.nyt.org.modules.OrderCancel.model.REQBODY2011;
import com.nyt.org.modules.OrderCancel.model.REQROOT2011;
import com.nyt.org.modules.OrderCancel.model.RSPROOT2011;
import com.nyt.org.modules.TicketStaion.service.FaService;
import com.nyt.org.modules.api.constant.ZhongLvCode;
import com.nyt.org.modules.api.model.XMLHead;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class OrderCancelDoService extends FaService {

    private Logger logger = LoggerFactory.getLogger(OrderCancelDoService.class);

    @Value("${zhonglv.server}")
    private String zhonglvURL;

    public Map<String,Object> orderCancelDo(JSONObject bodyJson) {
        Map<String,Object> map = new HashMap<>();

        REQROOT2011 root = new REQROOT2011();

        XMLHead head = super.bulldHEADXml(ZhongLvCode.订单取消);
        root.setHead(head);

        REQBODY2011 body = new REQBODY2011();
        REQ2011 req2011 = new REQ2011();
        req2011.setOrderNo(bodyJson.getString("OrderNo"));
        req2011.setForceFlag(bodyJson.getString("ForceFlag"));
        body.setReq2011(req2011);
        root.setBody(body);

        String reqXml = XmlUtils.toXml(root);
        logger.info("订单取消-接口[2011]请求报文：\n"+reqXml);

        String respXml = SendPostRequest.doPost(zhonglvURL,reqXml);
        logger.info("订单取消-接口[2011]响应报文：\n"+respXml);

        RSPROOT2011 rsproot2011 = XmlUtils.xmlToBean(respXml, RSPROOT2011.class);

        String jsonArray = JSONArray.toJSONString(rsproot2011.getBody().getRsp2011());

        net.sf.json.JSONArray jsonArray1 = JsonConvert.transToArray(jsonArray);

        System.out.println("-----------------------------------------------------");
        System.out.println(jsonArray1);
        map.put("rspCode",rsproot2011.getHead().getRspCode());
        map.put("rspMsg",rsproot2011.getHead().getRspMsg());
        map.put("responseBean",jsonArray1);

        return map;
    }



}
